<?php

class site_user_model extends CI_Model {

    protected $_instance = array(
        'table' => 'site_user',
        'primaryKey' => 'id',
    );
    protected $_parent = null;
    public $_data = null;

    public function get_user($reload = false) {
        $CI = & get_instance();
        $list_user = null;
        if ($reload) {
            $query = $this->db->get($this->_instance['table']);
            foreach ($query->result() as $row) {
                $list_user[] = $row;
            }
            $query->free_result();
            $CI->st_registry->setKey('site/user/collection', $list_user, GLOBAL_CACHE_DURATION);
        } else {
            $list_user = $CI->st_registry->getKey('site/user/collection');
            if (empty($list_user)) {
                return $this->get_user(true);
            }
        }
        return $list_user;
    }

    public function get_collection($start, $limit, $conds = null, $orders = null) {
        $query = 'SELECT * FROM ' . $this->_instance['table'];
        if (is_array($conds)) {
            $conds = implode(' AND ', $conds);
        }
        if (!empty($conds)) {
            $query .= ' WHERE ' . $conds;
        }
        if (!empty($orders)) {
            $query .= ' ORDER BY ' . $orders;
        }
        $query .= ' LIMIT ' . $start . ', ' . $limit;
        $query = $this->db->query($query);
        $list_user = array();
        foreach ($query->result() as $row) {
            $row->parts_name = '';
            $list_user[] = $row;
        }
        $query->free_result();
        return $list_user;
    }

    public function get_by_ids($ids) {
        if (empty($ids)) {
            return null;
        }
        $query = 'SELECT * FROM ' . $this->_instance['table'] . ' WHERE id IN ('.$ids.')';
        $rs = $this->db->query($query);
        $collection = $rs->result();
        $rs->free_result();
        return $collection;
    }

    public function add_user($data) {
        $CI = & get_instance();
        if (empty($data['username'])) {
            throw new Exception('Tên người dùng không được để trống');
            return false;
        }

        if (empty($data['error'])) {
            $data['created_date'] = time();
            $this->db->insert($this->_instance['table'], $data);
            // Update Cache
            $CI->st_registry->deleteKey('site/user/collection');
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function update_user($id, $data) {
        $CI = & get_instance();
        if (empty($data['username'])) {
            throw new Exception('Tên người dùng không được để trống');
            return false;
        }

        if (empty($data['error'])) {
            $data['updated_date'] = time();
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
            // Update Cache
            $CI->st_registry->deleteKey('site/user/collection');
            $CI->st_registry->deleteKey('site/user_by_id/' . $id);
            if (!empty($data['alias'])) {
                $CI->st_registry->deleteKey('site/user_by_id/' . $data['alias']);
            }
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function load_by_id($id, $reload = false) {
        $this->_data = null;
        $CI = & get_instance();
        if ($reload) {
            $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
            $this->_data = $query->row();
            $query->free_result();
            $CI->st_registry->setKey('site/user_by_id/' . $id, $this->_data, CACHE_TIMEOUT);
        } else {
            $this->_data = $CI->st_registry->getKey('site/user_by_id/' . $id);
            if (empty($this->_data)) {
                $this->_data = $this->load_by_id($id, true);
            }
        }
        return $this->_data;
    }

    public function delete_user($id) {
        $CI = & get_instance();
        $this->db->delete($this->_instance['table'], array('id' => $id));
        // Update Cache
        $CI->st_registry->deleteKey('site/user/collection');
        $CI->st_registry->deleteKey('site/user_by_id/' . $id);
        return $this;
    }

    /*private function auto_render($data) {
        $name = renderToAlias($data['name']);
        if (empty($data['alter_name'])) {
            $data['alter_name'] = $data['name'];
        }
        if (empty($data['meta_keywords'])) {
            $data['meta_keywords'] = $name;
        }
        if (empty($data['meta_title'])) {
            $data['meta_title'] = $data['name'];
        }
        if (empty($data['meta_description'])) {
            $data['meta_description'] = $data['name'];
        }
        return $data;
    }*/
}

?>